package entjava.poker;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entjava.poker.model.Card;
import entjava.poker.model.Deck;
import entjava.poker.model.Game;
import entjava.poker.model.Player;
import entjava.poker.service.PlayerService;
import entjava.poker.service.SmallBigService;

/**
 * Servlet implementation class GameServlet
 */
@WebServlet("/GameServlet")
public class GameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;    
	Game game;
	String turn;
	SmallBigService sbs;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ArrayList<Player> ls = (ArrayList<Player>)request.getServletContext().getAttribute("pList");	
		ServletContext context = request.getServletContext(); 
		System.out.println("gamezeroutsdev");

		if(ls.size()>=3){     
	//	if(ls.size()>=3 && game==null){                //problem we can't restart game;
		sbs=(SmallBigService)request.getServletContext().getAttribute("smallbig");
		
		game = new Game(ls,sbs);
		this.game.setGameStarted(true);		
		game.assignInitalCards();		
		game.getSmallBlindPlayer().putSmallBet();
		game.getBigBlindPlayer().putBigBet();
		game.setPot(30); //preset from betting
		
		System.out.println("gamezerv");

		
		String start = new String("Game is now on Progress ");
		context.setAttribute("start", start);
		
		turn = new String("Its Your Turn, " + game.getCurrentPlayer().getName());
		context.setAttribute("turn", turn);
		context.setAttribute("game", game);
		
		RequestDispatcher view = request.getRequestDispatcher("success.jsp");
		view.forward(request, response);
		
		}else{
		response.sendRedirect("success.jsp");
		}
		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ServletContext context = request.getServletContext(); 
		HttpSession session = request.getSession();
		String choose = request.getParameter("choose");
		
if(game!=null && choose!=null){
		System.out.println("game not null");
		game=(Game)request.getServletContext().getAttribute("game");
		
		String validatePlayer= game.getCurrentPlayer().getId();
		String id = session.getId();
		
	//determine if the current player is broke
	//	if(game.getCurrentPlayer().getMoney()==0){
	//		this.game.getCurrentPlayer().setAmBroke(true);
	//	}
				
		if(game.isGameStarted() && validatePlayer==id && this.game.isNotOver()){	
			System.out.println("game validate");
				if(choose.contains("call")){
					if(this.game.getCurrentPlayer().getBet()<this.game.getMinimumBet()){
						this.game.doBettingChoice(choose);
					}
				}else if(choose.contains("check")){
					if(this.game.getCurrentPlayer().getBet()==this.game.getMinimumBet()){
						this.game.doBettingChoice(choose);
					}
				}else if(choose.contains("fold")){
					this.game.doBettingChoice(choose);
				}else if(choose.contains("raise")){
					
					String snum=request.getParameter("raisetxt");
				//	int num= Integer.parseInt(request.getParameter("raisetxt")); 					//check if player is raising with blank txtfield or string
					
					if(snum!=null && snum.trim()!=""){
					System.out.println("inside the number parse int");
						int num= Integer.parseInt(snum); 	
						int dnum=this.game.getMinimumBet()+this.game.getMinimumBet(); 
					if(num<this.game.getCurrentPlayer().getMoney() && num >= dnum){
					this.game.setRaiseBet(num);
					this.game.doBettingChoice(choose);
					}
					}
				}//end of choose			
		
				System.out.println("You are on Round "+ this.game.getCountRounds());
				
				if(game.isNotOver()){
					turn = new String("Your Turn, " + game.getCurrentPlayer().getName());
					context.setAttribute("turn", turn);
				}else{
					turn = new String("Game Ended");
					context.setAttribute("turn", turn);
				}
			}//end validate
		}//end null		
				RequestDispatcher view = request.getRequestDispatcher("success.jsp");
				view.forward(request, response);
		
	}//end of post
}
